<?php
/**
  * Klasse Controller
  * @param  : $request, $template, $view
  *
  * @info   : Steuert die Anwendung
  *
  * @e.datum : 12.12.2010
  * @l.datum : 29.12.2010
  * @version: beta
 **/
class Controller{

  private $request = null;
  private $template = '';
  private $view = null;


  /**
    * Konstruktor, erstellet den Controller.
    * @param Array $request Array aus $_GET & $_POST.
    * @return
   **/
  public function __construct($request){
    $this->view = new View();
    $this->request = $request;
    $this->template = !empty($request['view']) ? $request['view'] : 'default';
  }

  
  /**
    * Methode zum anzeigen des Contents.
    * @param
    * @return String Content der Applikation.
  **/
  public function display(){
    if(empty($this->request['id'])||$this->request['id']<0){
      $this->template='formcontent';
      $this->request['id']=0;
    }

   
   if(isset($this->request['searchSchule'])){
     $_SESSION['UnLockSchule']="1";
   }

   if(isset($this->request['searchBetriebe'])){
     $_SESSION['UnLockBetriebe']="1";
   }

    if(isset($this->request['searchselect']) && !empty($_SESSION['search_result'])) {
        $searchtype = $_SESSION['search_result']['search'];
        switch($searchtype) {
            case "schule":
                $result = $_SESSION['search_result'][$this->request['searchselect']];
                $_SESSION['LSSchulFormSIM'] = ($result['SchulformBez']);
                $_SESSION['LSName'] = ($result['Name']);
                $_SESSION['LSStrasse'] = ($result['Strasse']);
                $_SESSION['LSPLZ'] = ($result['PLZ']);
                $_SESSION['LSOrt'] = ($result['Ort']);
                $_SESSION['LOCKSchule']="LSSchulFormSIM;LSName;LSStrasse;LSPLZ;LSOrt";
                $_SESSION['UnLockSchule']="0";
                break;
            case "betrieb":
                $result = $_SESSION['search_result'][$this->request['searchselect']];
                $_SESSION['AllgAdrName1'] = $result['AllgAdrName1'];
                $_SESSION['AllgAdrStrasse'] = $result['AllgAdrStrasse'];
                $_SESSION['AllgAdrPLZ'] = $result['AllgAdrPLZ'];
                $_SESSION['AllgAdrOrt'] = $result['AllgAdrOrt'];
                $_SESSION['AllgAdrTelefon1'] = $result['AllgAdrTelefon1'];
                $_SESSION['AllgAdrFax'] = $result['AllgAdrFax'];
                $_SESSION['AllgAdrEmail'] = $result['AllgAdrEmail'];
                $_SESSION['LOCKBetrieb']="AllgAdrName1;AllgAdrStrasse;AllgAdrPLZ;AllgAdrOrt;AllgAdrTelefon1;AllgAdrFax;AllgAdrEmail;";
                $_SESSION['UnLockBetriebe']="0";
                break;
            default:
                break;
        }
        if($this->request['id']-1 >= 0) {
            $this->request['id'] = $this->request['id']-1;
        }
    }

    if(isset($this->request['searchSchule'])||isset($this->request['searchBetriebe'])) {
        if($this->request['id']-1 >= 0) {
            $this->request['id'] = $this->request['id']-1;
        }
    }

    $view = new View();
    switch($this->template){
      case 'formcontent': {
        //mom. Maximal 4
        $max=5;

        //$_SESSION Daten
        $this->fuSessionData();    

        //Formularfelder
        $id = $this->request['id'];
        $colForms = Model::getColForms();
        $colForm;// = Model::getColForm($id);
        $Forms;
        if($id-1 >= 0) {
            $colLastForm = Model::getColForm($id-1);
            $Forms = Model::getForm($id-1);
        }

        //Vorbedingug ausführen
        if(isset($Forms[0]['precondition']) && $Forms[0]['precondition'] != "")
        {
            eval($Forms[0]['precondition']);
        }

        //Für Validierung
        if(isset($_SESSION['LOCKBetrieb'])||isset($_SESSION['LOCKSchule']))
            $colLastForm=$this->fuCheckLock($colLastForm);
        

        //Daten prüfen
        include_once ('classes/validation.php');
        $check = new vali($_SESSION, $colLastForm, $id);
        $id=$check->fuVali(&$colLastForm);

        if($id < $this->request['id']) {
            $colForm = $colLastForm;
        } else {
            $colForm = Model::getColForm($id);
        }

        //Nächstes Formular
        $Forms = Model::getForm($id);
        //$colData = Model::getColData($id);
        for ($index = 0; $index < count($colForm); $index++) {
            $colForm[$index]['data'] = Model::getColData($colForm[$index]['id']);
        }
        $nextId=$id+1;
        
        //Vorbedingug ausführen
        if(isset($Forms[0]['precondition']) && $Forms[0]['precondition'] != "")
        {
            eval($Forms[0]['precondition']);
        }

        if($nextId>$max){
          $view->setTemplate('overview');
          $formsWithCols = Model::getForms();
          for ($i = 0; $i < count($formsWithCols); $i++) {
              $formsWithCols[$i]['col'] = Model::getColForm($i);
          }
          $view->assign('forms', $formsWithCols);
        } else {
          if(isset($_SESSION['LOCKBetrieb'])||isset($_SESSION['LOCKSchule']))
            $colForm=$this->fuCheckLock($colForm);
          
          $view->setTemplate('formcontent');
          $view->assign('id', $nextId);
          $view->assign('aktId', $id);
          $view->assign('title', $Forms[0]['title']);
          $view->assign('subtitle', $Forms[0]['subtitle']);
          $view->assign('name', $Forms[0]['name']);
          $view->assign('colForms', $colForm);
          //$view->assign('coldata', $colData);
          $view->assign('test', $test);
        }
        break;
      } // case 'form' ..

      case 'overview': {
        $this->fuSessionData();   
        $view->setTemplate('overview');
        break;
      } // case 'overview' ..

      case 'finish': {
        $this->fuSessionData();
        $view->setTemplate('finish');
        break;
      } // case 'finish' ..

      case 'default': {

      } // case 'default' ..

      default: {
            $studies = Model::getStudies();
            $view->setTemplate('default');
            $view->assign('studies', $studies);
      } // default ..
    }
    if($nextId>$max){
      $this->view->setTemplate('main');
      $this->view->assign('main_title', 'Der Titel des Formulars');
      $this->view->assign('main_footer', '');
      $this->view->assign('main_content', $view->loadTemplate());
    } else {
      $this->view->setTemplate('form');
      $this->view->assign('form_title', 'Der Titel des Formulars');
      $this->view->assign('form_footer', '');
      $this->view->assign('form_content', $view->loadTemplate());
    }
    
    return $this->view->loadTemplate();
  } // switch($this->template) ..


  /**
    * Methode zum Laden der Sesseion daten
    * @param
    * @return
  **/
  private function fuSessionData(){
    foreach ($this->request as $k => $v) {
      $_SESSION[$k]=html_entity_decode($v, null, "utf-8");
    }
  }


   /**
    * Methode prüfen Suchfelder sperren
    * @param $colF
    * @return  $colF
  **/
  public static function fuCheckLock($colF){
    $i=0;
    $lock="".$_SESSION['LOCKSchule'];
    $fields=explode (";",$lock);
    //Schulen Sperren / Entsperren
    foreach($colF as $col){
      foreach($fields as $f){
        if(strcmp($f, $col['name'])==0){
          if($_SESSION['UnLockSchule']==1){
            $colF[$i]['readonly']=0;
            $colF[$i]['required']=1;
            $_SESSION['LSSchulFormSIM'] = null; $_SESSION['LSName'] = null; $_SESSION['LSStrasse'] = null;
            $_SESSION['LSPLZ']          = null; $_SESSION['LSOrt']  = null;
          } else {
            $colF[$i]['readonly']=1;
            $colF[$i]['required']=0;
          }

            break;
          }
       }
      $i++;
    }

    //Betriebe Sperren Entsperren
    $lock=null; $i=0;
    $lock="".$_SESSION['LOCKBetrieb'];
    $fields=explode (";",$lock);
    foreach($colF as $col){
      foreach($fields as $f){    
        if(strcmp($f, $col['name'])==0){
          if($_SESSION['UnLockBetriebe']==1){
            $colF[$i]['readonly']=0;
            $colF[$i]['required']=1;
            $_SESSION['AllgAdrName1']    = null;   $_SESSION['AllgAdrStrasse'] = null;
            $_SESSION['AllgAdrPLZ']      = null;   $_SESSION['AllgAdrOrt']     = null;
            $_SESSION['AllgAdrTelefon1'] = null;   $_SESSION['AllgAdrFax']     = null;
            $_SESSION['AllgAdrEmail']    = null;
          } else {
            $colF[$i]['readonly']=1;
            $colF[$i]['required']=0;
          }
        }
      }
     $i++;
    }
    return $colF;
  }
  
}
?>